package com.yihuan.controller;

import com.yihuan.entity.MoneyEntity;
import com.yihuan.entity.OrderEntity;
import com.yihuan.global.MessageConstant;
import com.yihuan.service.MoneyService;
import com.yihuan.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Author:梦醒
 * Date: 2022/4/24 18:01
 * Dream:
 * 信言不美，美言不信。
 * 善者不辩，辩者不善。
 * 知者不博，博者不知。
 * Function:
 */
@RestController
public class OrderController {
    int pageNumber = 0;
    int count = 0;
    //使用map存储数据
    private Map<String, Object> map = new HashMap<>();
    //数据页数和起始值
    private int num;
    @Autowired
    OrderService orderService;
    @Autowired
    MoneyService moneyService;
    
    /**
     * 新增数据
     *
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("/insertOrderByOrderData")
    public Map<String, Object> insertOrderByOrderData(@RequestBody OrderEntity model, HttpSession session) {
        model.setUserID((Integer) session.getAttribute("UserID_4"));
        //设置时间的格式
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //获取时间的方法
        Date d = new Date();
        //将获取的时间转换成设置的时间格式进行存储
        model.setOrderDate(sf.format(d));
        //状态
        model.setOrderState(1);
        //公司编号
        model.setCompID((Integer) session.getAttribute("CompID_1"));
        String key = String.valueOf((long) ((Math.random() * 9 + 1) * 100000000));
        for (int i = 0; i <= 2; i++) {
            key += String.valueOf((long) ((Math.random() * 9 + 1) * 100000000));
        }
        //编号
        model.setOrderName(key);
        int insertOrderByOrderData = orderService.insertOrderByOrderData(model);
        if (insertOrderByOrderData == 0) {
            map.put("code", MessageConstant.FALL_CODE);
            map.put("explain ", MessageConstant.ORDER_INSERT_FALL);
            return map;
        }
        List<OrderEntity> list = orderService.findOrderByOrderAndOrderDate(model);
        session.setAttribute("orderID", list.get(0).getOrderID());
        map.put("code", MessageConstant.SUCCESS_CODE);
        map.put("explain ", MessageConstant.ORDER_INSERT_SUCCESS);
        return map;
    }
    
    /**
     * 修改订单状态
     *
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("/updateOrderByOrderState")
    public Map<String, Object> updateOrderByOrderState(@RequestBody OrderEntity model, HttpSession session) {
        int updateOrderByOrderState = orderService.updateOrderByOrderState(model);
        if (model.getOrderState() == 4) {
            List<OrderEntity> findOrderPriceByOrderPrice = orderService.findOrderPriceByOrderPrice(model);
            MoneyEntity money = new MoneyEntity();
            //设置时间的格式
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            //获取时间的方法
            Date d = new Date();
            //将获取的时间转换成设置的时间格式进行存储
            money.setUserMoneyDate(sf.format(d));
            money.setUserID(findOrderPriceByOrderPrice.get(0).getUserID());
            money.setUserMoneyPrice((double) (5 + Math.random() * (8 - 5 + 1)));
            money.setUsersID(findOrderPriceByOrderPrice.get(0).getUsersID());
            moneyService.insertMoneyByMoneyData(money);
        }
        if (updateOrderByOrderState == 0) {
            map.put("code", MessageConstant.FALL_CODE);
            map.put("explain ", MessageConstant.ORDER_UPDATE_FALL);
            return map;
        }
        map.put("code", MessageConstant.SUCCESS_CODE);
        map.put("explain ", MessageConstant.ORDER_UPDATE_SUCCESS);
        return map;
    }
    
    /**
     * 查询对应公司的订单信息
     *
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("/findOrderByOrderUsersData")
    public Map<String, Object> findOrderByOrderUsersData(@RequestBody OrderEntity model, HttpSession session) {
        List<OrderEntity> findOrderByOrderUsersData = orderService.findOrderByOrderUsersData(model);
        if (findOrderByOrderUsersData.size() == 0) {
            map.put("code", MessageConstant.FALL_CODE);
            map.put("explain ", MessageConstant.ORDER_UPDATE_FALL);
            return map;
        }
        map.put("code", MessageConstant.SUCCESS_CODE);
        map.put("order_list", findOrderByOrderUsersData);
        map.put("explain ", MessageConstant.ORDER_UPDATE_SUCCESS);
        return map;
    }
    
    /**
     * 修改订单状态 接单员编号
     *
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("/updateOrderByUsersID")
    public Map<String, Object> updateOrderByUsersID(@RequestBody OrderEntity model, HttpSession session) {
        //接单编号
        // model.setUsersID((Integer) session.getAttribute("UserID_3"));
        model.setUsersID(7);
        int updateOrderByUsersID = orderService.updateOrderByUsersID(model);
        if (updateOrderByUsersID == 0) {
            map.put("code", MessageConstant.FALL_CODE);
            map.put("explain ", MessageConstant.ORDER_UPDATE_FALL);
            return map;
        }
        map.put("code", MessageConstant.SUCCESS_CODE);
        map.put("explain ", MessageConstant.ORDER_UPDATE_SUCCESS);
        return map;
    }
    
    /**
     * 查询对应公司的订单信息
     *
     * @param session
     * @return
     */
    @RequestMapping("/findOrderByOrderStateData")
    public Map<String, Object> findOrderByOrderStateData(HttpSession session) {
        //接单编号
        // model.setUsersID((Integer) session.getAttribute("UserID_3"));
        OrderEntity model = new OrderEntity();
        model.setUsersID(7);
        List<OrderEntity> findOrderByOrderStateData = orderService.findOrderByOrderStateData(model);
        if (findOrderByOrderStateData.size() == 0) {
            map.put("code", MessageConstant.FALL_CODE);
            map.put("explain ", MessageConstant.ORDER_UPDATE_FALL);
            return map;
        }
        map.put("code", MessageConstant.SUCCESS_CODE);
        map.put("order_list", findOrderByOrderStateData);
        map.put("explain ", MessageConstant.ORDER_UPDATE_SUCCESS);
        return map;
    }
    
    /**
     * 修改订单总价 接单员编号
     *
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("/updateOrderByOrderPrice")
    public Map<String, Object> updateOrderByOrderPrice(@RequestBody OrderEntity model, HttpSession session) {
        int updateOrderByUsersID = orderService.updateOrderByOrderPrice(model);
        if (updateOrderByUsersID == 0) {
            map.put("code", MessageConstant.FALL_CODE);
            map.put("explain ", MessageConstant.ORDER_UPDATE_FALL);
            return map;
        }
        map.put("code", MessageConstant.SUCCESS_CODE);
        map.put("explain ", MessageConstant.ORDER_UPDATE_SUCCESS);
        return map;
    }
    
    /**
     * 查询不同状态下的订单
     *
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("/findOrderByOrderData")
    public Map<String, Object> findOrderByOrderData(@RequestBody OrderEntity model, HttpSession session) {
        model.setUserID((Integer) session.getAttribute("UserID_4"));
        List<OrderEntity> findOrderByOrderData = orderService.findOrderByOrderData(model);
        if (findOrderByOrderData.size() == 0) {
            map.put("code", MessageConstant.FALL_CODE);
            map.put("explain ", MessageConstant.ORDER_UPDATE_FALL);
            return map;
        }
        map.put("code", MessageConstant.SUCCESS_CODE);
        map.put("order_list", findOrderByOrderData);
        map.put("explain ", MessageConstant.ORDER_UPDATE_SUCCESS);
        return map;
    }
    
    /**
     * 分页查询数据
     *
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("/findOrderByOrderList")
    public Map<String, Object> findOrderByOrderList(@RequestBody OrderEntity model, HttpSession session) {
        model.setCompID((Integer) session.getAttribute("compID"));
        List<OrderEntity> findOrderByOrderList = orderService.findOrderByOrderList(model);
        if (findOrderByOrderList.size() == 0) {
            map.put("code", MessageConstant.FALL_CODE);
            map.put("explain ", MessageConstant.RECSTYLE_PRODUCT_LIMIT_FALL);
            return map;
        }
        pageNumber = countRecycleProductList(model);
        map.put("code", MessageConstant.SUCCESS_CODE);
        map.put("order_List", findOrderByOrderList);
        map.put("pageNumber", pageNumber);
        map.put("count", count);
        map.put("explain ", MessageConstant.RECSTYLE_PRODUCT_LIMIT_SUCCESS);
        return map;
    }
    
    /**
     * 统计分页查询数据
     *
     * @param model
     * @return
     */
    public int countRecycleProductList(OrderEntity model) {
        count = orderService.countOrderList(model);
        if (count % model.getNumber() == 0) {
            pageNumber = count / model.getNumber();
        } else {
            pageNumber = count / model.getNumber() + 1;
        }
        return pageNumber;
    }
    
    /**
     * 成交金额
     *
     * @return
     */
    @RequestMapping("/sumOrderListByCompID")
    public Map<String, Object> sumOrderListByCompID(HttpSession session) {
        OrderEntity model = new OrderEntity();
        model.setCompID((Integer) session.getAttribute("compID"));
        double sumOrderListByCompID = orderService.sumOrderListByCompID(model);
        map.put("code", MessageConstant.SUCCESS_CODE);
        map.put("sumOrder", sumOrderListByCompID);
        map.put("explain ", MessageConstant.ORDER_UPDATE_SUCCESS);
        return map;
    }
    
    /**
     * 总体成交金额
     *
     * @return
     */
    @RequestMapping("/sumOrderList")
    public Map<String, Object> sumOrderList(HttpSession session) {
        OrderEntity model = new OrderEntity();
        double sumOrderList = orderService.sumOrderList(model);
        map.put("code", MessageConstant.SUCCESS_CODE);
        map.put("sumOrder", sumOrderList);
        map.put("explain ", MessageConstant.ORDER_UPDATE_SUCCESS);
        return map;
    }
    
    /**
     * 订单
     *
     * @return
     */
    @RequestMapping("/sumOrderListByCompIDAndOrderState")
    public Map<String, Object> sumOrderListByCompIDAndOrderState(@RequestBody OrderEntity model, HttpSession session) {
        model.setCompID((Integer) session.getAttribute("compID"));
        double sumOrderListByCompID = orderService.sumOrderListByCompIDAndOrderState(model);
        map.put("code", MessageConstant.SUCCESS_CODE);
        map.put("sumOrderList", sumOrderListByCompID);
        map.put("explain ", MessageConstant.ORDER_UPDATE_SUCCESS);
        return map;
    }
    
    /**
     * 总体订单
     *
     * @return
     */
    @RequestMapping("/sumOrderListAndOrderState")
    public Map<String, Object> sumOrderListAndOrderState(@RequestBody OrderEntity model, HttpSession session) {
        double sumOrderList = orderService.sumOrderListAndOrderState(model);
        map.put("code", MessageConstant.SUCCESS_CODE);
        map.put("sumOrderList", sumOrderList);
        map.put("explain ", MessageConstant.ORDER_UPDATE_SUCCESS);
        return map;
    }
}
